package top.healthylife.controller;/*
 *@Author GaoZeXi
 *@Created time 2020/4/10 15:42
 *@Description:
 * Step by Step  and Stand on,  You Are The Best Investment!!!
 */

import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import top.healthylife.constant.MessageConstant;
import top.healthylife.domain.OrderSetting;
import top.healthylife.entity.Result;
import top.healthylife.service.OrderSettingService;
import top.healthylife.utils.POIUtils;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/ordersetting")
public class OrderSettingController {


    @Reference
    private OrderSettingService orderSettingService;

    /**
       * Excel文件上传，并解析文件内容保存到数据库
       * @param excelFile
       * @return
       */
    @RequestMapping("/upload")
    public Result upload(@RequestParam("excelFile")MultipartFile multipartFile){
        try {
            List<String[]> list = POIUtils.readExcel(multipartFile);
            if(list !=null && list.size()>0){
                List<OrderSetting> orderSettings = new ArrayList<>();
                OrderSetting orderSetting = new OrderSetting();
                //封装到List<OrderSetting>对象中
                for (String[] strings : list) {
                    Date orderDate =new SimpleDateFormat("yyyy/MM/dd").parse(strings[0]);
                    Integer number =Integer.parseInt(strings[1]);
                    orderSetting = new OrderSetting(orderDate,number);
                    orderSettings.add(orderSetting);
                    orderSetting = null;
                }
                orderSettingService.add(orderSettings);
            }

        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        } catch (ParseException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
        return new Result(true,MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
    }


    @RequestMapping("/getOrderSettingByMonth")
    public Result getOrderSettingByMonth(String date){//2019-04

        try {
            List<Map> list =orderSettingService.getOrderSettingByMonth(date);
            return new Result(true,MessageConstant.GET_ORDERSETTING_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_ORDERSETTING_FAIL);
        }
    }

    @RequestMapping("/updateNumberByDate")
    public Result updateNumberByDate(@RequestBody OrderSetting orderSetting){//2019-04-01
        try {
            orderSettingService.updateNumberByDate(orderSetting);
            return new Result(true,MessageConstant.ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ORDERSETTING_FAIL);
        }
    }
}
